import request from '@/utils/request'
import { removeToken, setToken } from '@/utils/token'
import { LOGIN, LOGOUT } from '../constants'
//发送请求获取 这里的action 要执行必须去组建中dispatch一下
export const login = (mobile, code) => {
    //发送请求
    return async dispatch => {
        const res = await request.post('/authorizations', { mobile, code })
        // console.log(res);
        const token = res.data.data.token
        setToken(token)
        // console.log(res);
        //成功获取到了数据要存起来
        dispatch({
            type: LOGIN,
            payload: token//传过去的数据
        })
    }
}


//退出
export const logout = () => {
    //清除本地的token
    removeToken()
    //定义rudex中的token
    return {
        type: LOGOUT
    }
}